//------------------------------------------------------------------------------------
#include "CMainWindow.h"
#include "common.h"
//------------------------------------------------------------------------------------
CMainWindow::CMainWindow(QWidget *parent) : QMainWindow(parent) {
    setupUi(this);

    timer = new QTimer(this);
    timerTps = new QTimer(this);
	fondPlayer = new CSoundPlayer(":/snake/sounds/fond.mp3", this);
	mangePlayer = new CSoundPlayer(":/snake/sounds/souris.mp3", this);
	oneUpPlayer = new CSoundPlayer(":/snake/sounds/1up.mp3", this);
	lostLifePlayer = new CSoundPlayer(":/snake/sounds/lostLife.mp3", this);
	pausePlayer = new CSoundPlayer(":/snake/sounds/pause.mp3", this);
	gameOverPlayer = new CSoundPlayer(":/snake/sounds/gameOver.mp3", this);

    vitesse = 300;
	temps = 0;
	nbVie = START_NBVIE;
	inPause = false;

    wGamePlay->setFocus();

	//TODO screen
	fondPlayer->play(true);
    connect(timer, SIGNAL(timeout()), this, SLOT(onTimerTimeout()));
    timer->start(vitesse);

    connect(timerTps, SIGNAL(timeout()), this, SLOT(onTimerTpsTimeout()));
    timerTps->start(SECOND);
}
//------------------------------------------------------------------------------------
CMainWindow::~CMainWindow() {
    timer->stop();
	timerTps->stop();

	fondPlayer->stop();

    delete timer;
	delete timerTps;
	delete fondPlayer;
	delete mangePlayer;
	delete oneUpPlayer;
	delete lostLifePlayer;
	delete pausePlayer;
	delete gameOverPlayer;
}
//------------------------------------------------------------------------------------
void CMainWindow::onTimerTimeout(void) {
    wGamePlay->next();
}
//------------------------------------------------------------------------------------
void CMainWindow::onTimerTpsTimeout(void) {
	wGamePlay->setTemps(++temps);
}
//------------------------------------------------------------------------------------
void CMainWindow::on_wGamePlay_die(void) {
	nbVie--;
	if(nbVie > 0) {
		//TODO screen
		wGamePlay->setNbVie(nbVie);
		lostLifePlayer->play(false);
	}else {
		timer->stop();
		timerTps->stop();
		fondPlayer->stop();
		gameOverPlayer->play(false);

		//TODO replay
	}
}
//------------------------------------------------------------------------------------
void CMainWindow::on_wGamePlay_mangeSouris(void) {
	mangePlayer->play(false);
}
//------------------------------------------------------------------------------------
void CMainWindow::on_wGamePlay_pause(void) {
	inPause = !inPause;
	if(inPause) {
		pausePlayer->play(false);
		//TODO show pause
	}else {
		//TODO hide pause
	}
}
//------------------------------------------------------------------------------------
